import { createRouter, createWebHistory } from 'vue-router';
import { ElNotification } from 'element-plus'

const routes = [
    {
        path: '/',
        name: 'load',
        component: () => import('@/views/login/Index.vue')
    },
    {
        path: '/home',
        name: 'home',
        component: () => import('@/views/home/Index.vue'),
        children: [
            {
                path: 'shouye',
                name: 'shouye',
                component: () => import('@/views/Shouye.vue'),
            },
            {
                path: 'peopleMsg',
                name: 'peopleMsg',
                component: () => import('@/views/NewPeopleMsg.vue')
            },
            {
                path: 'active',
                name: 'active',
                component: () => import('@/views/Actives.vue')
            },
            {
                path: 'changePage',
                name: 'changePage',
                component: () => import('@/views/ChangeActives.vue'),
            },
            {
                path: 'classes',
                name: 'classes',
                component: () => import('@/views/Classes.vue'),
            },
            {
                path: 'mangeClass',
                name: 'mangeClass',
                component: () => import('@/views/MangeClass.vue')
            },
            {
                path: 'moreClass',
                name: 'moreClass',
                component: () => import('@/views/MoreClass.vue'),
            },
            {
                path: 'video',
                name: 'video',
                component: () => import('@/views/VideoMange.vue'),
            },
            {
                path: 'changeUser',
                name: 'changeUser',
                component: () => import('@/views/ChangeInfo.vue'),
            },
            {
                path: 'index',
                name: 'index',
                component: () => import('@/views/Index.vue'),
            }
        ]

    },
    {
        path: '/:pathMatch(.*)*',
        name: 'NotFind',
        component: () => import('@/views/404/NotFined.vue')
    }
];

let router = createRouter({
    routes,
    history: createWebHistory()
})

router.beforeEach((to, form, next) => {
    let path = to.path;
    let bol = false;
    if (path == '/load' || path == '/') {
        bol = true;
    }
    if (!bol && !Boolean(window.localStorage.getItem('token'))) {
        ElNotification({
            title: 'WARING',
            message: '请登录！',
            type: 'warning',
        })
        return next('/');
    } else {
        if (Boolean(localStorage.getItem('token')) && bol) {
            ElNotification({
                title: 'WARING',
                message: '已登录，请退出登陆！',
                type: 'warning',
            })
            return next('/home/shouye')
        } else {
            return next();
        }
    }
})

export default router;
